<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <title>文件管理</title>
    <%@ include file="/commons/global.jsp" %>
    <%@ include file="/commons/basejs.jsp" %>
    <style type="text/css">
        #doc_table li{
            position: relative;
            float: left;
            margin: 0px 0px 20px 20px;
        }
        .file{
            width: 100px;
            text-align: center;
            height: 105px;
        }
    </style>
    <script type="text/javascript">
        var $this;
        $(function(){
            //flag标志位1-向前 -1向后
            getDocs('',1);
            contextMenu();
        })

        function contextMenu(){
            $('.file').on('contextmenu',function (e) {
                 $this=$(this);
                e.preventDefault();
                $('#menu').menu('show',{
                    left:e.pageX,
                    top:e.pageY
                })
            })
        }





        /**
         * 还原文件
         */
        function backTo(){
            var txt=$this.find('span').attr('title');
            var id=$this.data('id');
            var tip='确认要还原【'+txt+'】？';
            if($this.hasClass('dir')){
                tip='确认要还原【'+txt+'】,还原后会将该文件下所有文件还原？'
            }
            parent.$.messager.confirm('询问', tip, function(b) {
                if (b) {
                    progressLoad();
                    var url='${path }/doc/updateBack/'+id;
                    $.post(url, {

                    }, function(result) {
                        if (result.success) {
                            showMsg(result.msg);
                            $this.remove();
                        } else {
                            parent.$.messager.alert('错误', result.msg, 'error');
                        }
                        progressClose();
                    }, 'JSON');
                }
            });
        }

        /**
         * 还原文件
         */
        function remove(){
            var txt=$this.find('span').attr('title');
            var id=$this.data('id');
            var tip='确认要删除【'+txt+'】？';
            if($this.hasClass('dir')){
                tip='确认要删除【'+txt+'】,删除后会将该文件下所有文件删除？'
            }
            parent.$.messager.confirm('询问', tip, function(b) {
                if (b) {
                    progressLoad();
                    var url='${path }/doc/delete/'+id;
                    $.post(url, {

                    }, function(result) {
                        if (result.success) {
                            showMsg(result.msg);
                            $this.remove();
                        } else {
                            parent.$.messager.alert('错误', result.msg, 'error');
                        }
                        progressClose();
                    }, 'JSON');
                }
            });
        }

        /**
         * 返回上一级
         */
        function backDoc(){
            var id=$('#prev').val();
          if(!isNull(id)){
            $.ajax({
                url:'${ctxPath}/doc/get/'+id,
                dataType:'json',
                success:function(data){
                    if(data.success){
                        var pid=data.obj.pid;
                        getDocs(pid,-1);
                    }
                }
            })
          }else{
              getDocs("",-1);
          }
        }

        /**
         * 刷新
         */
        function refreshDoc(){
                var pid = $('#prev').val();
                getDocs(pid, 1);
        }




        /**
         * 获取数据
         */
        function getDocs(pid,flag){
            progressLoad();
                $.ajax({
                    url:'${ctxPath}/doc/findByPid',
                    data:{pid:pid,status:-1},
                    type:'post',
                    dataType:'json',
                    success:function(result){
                        progressClose();
                        if(result.success){
                            var data=result.obj;
                            render(data,flag);
                        }
                    },
                    error:function (e) {
                        progressClose();
                    }
                })
        }
        function openDir(id,pid,path){
            $.ajax({
                url:'${ctxPath}/doc/get/'+id,
                dataType:'json',
                success:function(data) {
                    if (data.success) {
                        var pid = data.obj.pid;
                        var name=data.obj.name;
                        var cur_location=$('#cur_location').text();
                        console.log(cur_location)
                        if(cur_location=="/"){
                            cur_location=cur_location+name;
                        }else{
                            cur_location=cur_location+"/"+name;
                        }
                        $('#cur_location').html(cur_location);
                        $('#prev').val(id);
                        getDocs(id,1);
                    }
                }
            })

        }
        function render(data,flag) {
            $('#doc_table').html('');
            var html="";
            if(data.length>0){
                if(flag==-1){
                    $('#prev').val(data[0].pid=='null'?"":data[0].pid);
                    var cur_location=$('#cur_location').text();
                    cur_location=cur_location.substr(0,cur_location.lastIndexOf('/'));
                    if(cur_location==""){
                        cur_location="/";
                    }
                    $('#cur_location').text(cur_location);
                }
                for(var i=0;i<data.length;i++){
                    if(data[i].type==1){//文件夹
                             html+='<li class="file dir" data-id="'+data[i].id+'" ondblclick="openDir(\''+data[i].id+'\',\''+data[i].pid+'\',\''+data[i].path+'\')"><img src="${staticPath}/static/img/doc/dir.png" alt="" title="'+data[i].name+'">'
                        +'<br>'
                        +'<span title="'+data[i].name+'">'+str2span(data[i].name,18)+'</span>'
                        +' </li>';
                    }else{//文件
                        if(data[i].fileType=='zip' || data[i].fileType=='htm' || data[i].fileType=='html' || data[i].fileType=='pdf' || data[i].fileType=='mp4' || data[i].fileType=='mp3'
                            ||data[i].fileType=='json'|| data[i].fileType=='ppt'|| data[i].fileType=='xls' || data[i].fileType=='txt'||data[i].fileType=='xlsx'||data[i].fileType=='doc'||data[i].fileType=='py'
                            ||data[i].fileType=='vue'||data[i].fileType=='sql'||data[i].fileType=='js'||data[i].fileType=='java'||data[i].fileType=='xml'||data[i].fileType=='css'){
                            html+='<li class="file" data-id="'+data[i].id+'"><img src="${staticPath}/static/img/doc/'+data[i].fileType+'.png" alt="" title="'+data[i].name+'  '+data[i].size+'">'
                                +'<br>'
                                +'<span title="'+data[i].name+'">'+str2span(data[i].name,18)+'</span>'
                                +' </li>';
                        }else if(data[i].fileType=='c' || data[i].fileType=='cpp'){
                            html+='<li class="file" data-id="'+data[i].id+'"><img src="${staticPath}/static/img/doc/code.png" alt="" title="'+data[i].name+'  '+data[i].size+'">'
                                +'<br>'
                                +'<span title="'+data[i].name+'">'+str2span(data[i].name,18)+'</span>'
                                +' </li>';
                        }else if(data[i].fileType=='rar'){
                            html+='<li class="file" data-id="'+data[i].id+'"><img src="${staticPath}/static/img/doc/rar.png" alt="" title="'+data[i].name+'  '+data[i].size+'">'
                                +'<br>'
                                +'<span title="'+data[i].name+'">'+str2span(data[i].name,18)+'</span>'
                                +' </li>';
                        }else{
                            html+='<li class="file" data-id="'+data[i].id+'"><img src="${staticPath}/static/img/doc/file.png" alt="" title="'+data[i].name+'  '+data[i].size+'">'
                                +'<br>'
                                +'<span title="'+data[i].name+'">'+str2span(data[i].name,18)+'</span>'
                                +' </li>';
                        }
                    }
                }
            }
            $('#doc_table').append(html);
            contextMenu();
        }




    </script>
</head>
<body>
<div class="easyui-layout" data-options="fit:true,border:false" >
    <div data-options="region:'center',border:false" style="padding: 10px;">
        <div style="border-bottom: 1px solid #eee8d5;padding: 10px;">当前位置:
        <span id="cur_location">/</span>
            <input type="hidden"  id="prev" value="">
        </div>
        <div style="padding: 10px;">
            <button class="easyui-linkbutton"  data-options="iconCls:'fi-arrow-left  icon-grey'" onclick="backDoc()">返回</button>
            <button class="easyui-linkbutton" data-options="iconCls:'fi-loop  icon-grey'" onclick="refreshDoc()">刷新</button>
        </div>
        <div style="padding: 10px;padding-left:0px;">
            <ul style="list-style: none;" id="doc_table">
            </ul>
        </div>
        <div id="menu" class="easyui-menu" style="width:120px;display:none">
            <div onclick="backTo()" data-options="iconCls:'fi-marker'">还原</div>
            <div onclick="remove()" data-options="iconCls:'fi-minus'">删除</div>
        </div>

    </div>
</div>
</body>
</html>